跳转至

pgvector v0.7.0 的新特性解析

文章背景与核心概要

随着向量检索在人工智能和机器学习领域的广泛应用,如何降低高维向量带来的巨大内存和存储开销成为了数据库优化的关键。pgvector v0.7.0 版本的发布标志着该扩展在性能和资源利用率上实现了重大飞跃。

该版本通过引入半精度浮点向量(float16)、稀疏向量(sparsevec)以及二进制量化(Bit vectors),有效地减少了向量空间中的冗余,在维持高精度的同时,实现了高达 50% 的内存与存储空间节约。此外,新增的距离函数和并行构建优化使整体性能较以往版本提升了 100 倍以上,为开发者构建经济高效且具备规模扩展能力的 AI 应用提供了强有力的底层支持。


Float16 向量 (Float16 Vectors)

Historically, pgvector relied on 32-bit vectors. Version 0.7.0 introduces 16-bit float (halfvec) HNSW indexes, which consume exactly half the memory of their 32-bit counterparts. This allows more vectors to fit into shared memory, reducing costly I/O operations and page evictions.

Implementation Example:

-- Create a table using half-precision vectors
create table embedding_half (
  id serial,
  vector halfvec(1536),
  primary key (id)
);

-- Migrate data from float32 to float16
insert into embedding_half (vector)
select vector::halfvec(1536) from embedding_full;

-- Build a float16 HNSW index
create index on embedding_half using hnsw (vector halfvec_l2_ops);
Performance Note: Parallel HNSW builds are approximately 30% faster with halfvec, and total storage requirements for both heap and HNSW relations are reduced by 50%.

历史上的 pgvector 一直依赖 32 位向量。0.7.0 版本引入了 16 位浮点(halfvec)HNSW 索引,其内存消耗仅为 32 位向量的一半。这使得更多的向量能够放入共享内存中,从而减少了高昂的 I/O 操作和页面驱逐。

实现示例:

-- Create a table using half-precision vectors
create table embedding_half (
  id serial,
  vector halfvec(1536),
  primary key (id)
);

-- Migrate data from float32 to float16
insert into embedding_half (vector)
select vector::halfvec(1536) from embedding_full;

-- Build a float16 HNSW index
create index on embedding_half using hnsw (vector halfvec_l2_ops);
性能说明: 使用 halfvec 时,并行 HNSW 构建速度大约快 30%,并且堆表(heap)和 HNSW 关系的总存储需求减少了 50%。


稀疏向量 (Sparse Vectors)

For datasets containing many zero components, sparsevec provides a storage-efficient representation by only storing non-zero values.

Usage:

create table embedding_sparse (
  id serial,
  vector sparsevec(1536),
  primary key (id)
);

-- Insert sparse data
insert into embedding_sparse (vector) 
values ('{1:0.1,3:0.2,5:0.3}/1536'), ('{1:0.4,3:0.5,5:0.6}/1536');

-- Query using sparse syntax
select * from embedding_sparse order by vector <-> '{1:3,3:1,5:2}/1536' limit 5;

对于包含许多零分量的数据集,sparsevec 仅存储非零值,从而提供了一种高效的存储表示方式。

使用方法:

create table embedding_sparse (
  id serial,
  vector sparsevec(1536),
  primary key (id)
);

-- Insert sparse data
insert into embedding_sparse (vector) 
values ('{1:0.1,3:0.2,5:0.3}/1536'), ('{1:0.4,3:0.5,5:0.6}/1536');

-- Query using sparse syntax
select * from embedding_sparse order by vector <-> '{1:3,3:1,5:2}/1536' limit 5;


二进制向量 (Bit Vectors)

Binary quantization allows float vectors to be represented in binary space. This is ideal for "pre-selecting" candidates from a large dataset before performing a more precise search.

Workflow for Pre-selection:

select * from (
  select * from embedding
  order by binary_quantize(vector)::bit(3) <~> binary_quantize('[1,-2,3]')
  limit 20
)
order by vector <=> '[1,-2,3]'
limit 5;

二进制量化允许将浮点向量表示在二进制空间中。这非常适合在执行更精确的搜索之前,从大型数据集中“预选”候选对象。

预选工作流:

select * from (
  select * from embedding
  order by binary_quantize(vector)::bit(3) <~> binary_quantize('[1,-2,3]')
  limit 20
)
order by vector <=> '[1,-2,3]'
limit 5;


新增距离函数 (New Distance Functions)

pgvector 0.7.0 expands its mathematical capabilities with support for: * L1 Distance (<+>): Taxicab geometry. * Hamming Distance: For bit vectors. * Jaccard Distance: For set similarity.

Indexing examples:

create index on items using hnsw (embedding vector_l1_ops);
create index on items using hnsw (embedding bit_hamming_ops);
create index on vector using hnsw (vector bit_jaccard_ops);

pgvector 0.7.0 扩展了其数学计算能力,支持以下功能: * L1 距离 (<+>):出租车几何(曼哈顿距离)。 * 汉明距离(Hamming Distance):用于二进制向量。 * 杰卡德距离(Jaccard Distance):用于集合相似度。

索引示例:

create index on items using hnsw (embedding vector_l1_ops);
create index on items using hnsw (embedding bit_hamming_ops);
create index on vector using hnsw (vector bit_jaccard_ops);


结论 (Conclusion)

With the addition of halfvec, sparsevec, and binary quantization, pgvector continues to evolve into a highly performant vector database engine. These features enable developers to scale their AI applications while significantly reducing infrastructure costs.

Using v0.7.0 in Supabase

All new Supabase projects ship with pgvector v0.7.0+. To enable the extension:

create extension if not exists vector
with schema extensions;

If you are on an older version, you can upgrade by navigating to the Infrastructure page in your project dashboard and updating your Postgres version to 15.1.1.47 or later.

随着 halfvecsparsevec 和二进制量化的加入,pgvector 正继续演进为一个高性能的向量数据库引擎。这些特性使开发者能够在显著降低基础设施成本的同时,扩展其 AI 应用规模。

在 Supabase 中使用 v0.7.0

所有新建的 Supabase 项目均已默认搭载 pgvector v0.7.0+。要启用该扩展,请运行:

create extension if not exists vector
with schema extensions;

如果你使用的是旧版本,可以通过导航到项目仪表板中的 Infrastructure(基础设施) 页面,并将 Postgres 版本更新为 15.1.1.47 或更高版本来进行升级。